import time
from alg import restore_ip_addresses, restore_ip_addresses_optimized, solve_ip_restoration

def test_basic_cases():
    """测试基本用例"""
    print("=== 基本测试用例 ===")
    
    test_cases = [
        ("25525511135", ["255.255.11.135", "255.255.111.35"]),
        ("0000", ["0.0.0.0"]),
        ("010010", ["0.10.0.10", "0.100.1.0"]),
        ("101023", ["1.0.10.23", "1.0.102.3", "10.1.0.23", "10.10.2.3", "101.0.2.3"]),
    ]
    
    for i, (input_str, expected) in enumerate(test_cases):
        result = restore_ip_addresses(input_str)
        result.sort()
        expected.sort()
        
        print(f"测试用例 {i+1}:")
        print(f"  输入: '{input_str}'")
        print(f"  期望: {expected}")
        print(f"  结果: {result}")
        print(f"  通过: {'✓' if result == expected else '✗'}")
        print()

def test_edge_cases():
    """测试边界情况"""
    print("=== 边界测试用例 ===")
    
    edge_cases = [
        ("", []),  # 空字符串
        ("1", []),  # 太短
        ("12", []),  # 太短
        ("123", []),  # 太短
        ("1234", ["1.2.3.4"]),  # 最短有效
        ("192168001001", ["192.168.0.01", "192.168.00.1", "19.216.80.01", "19.216.800.1"]),  # 前导0情况
        ("256256256256", []),  # 所有段都超过255
        ("999999999999", []),  # 太大
        ("000000000000", []),  # 全0但有前导0
    ]
    
    for i, (input_str, expected) in enumerate(edge_cases):
        result = restore_ip_addresses(input_str)
        result.sort()
        expected.sort()
        
        print(f"边界测试 {i+1}:")
        print(f"  输入: '{input_str}'")
        print(f"  期望: {expected}")
        print(f"  结果: {result}")
        print(f"  通过: {'✓' if result == expected else '✗'}")
        print()

def test_performance():
    """性能测试"""
    print("=== 性能测试 ===")
    
    test_strings = [
        "25525511135",
        "192168001001",
        "111111111111",
        "123456789012"
    ]
    
    for test_str in test_strings:
        print(f"测试字符串: '{test_str}'")
        
        # 测试基本版本
        start_time = time.time()
        result1 = restore_ip_addresses(test_str)
        time1 = time.time() - start_time
        
        # 测试优化版本
        start_time = time.time()
        result2 = restore_ip_addresses_optimized(test_str)
        time2 = time.time() - start_time
        
        print(f"  基本版本: {len(result1)} 个结果, 耗时: {time1:.6f}s")
        print(f"  优化版本: {len(result2)} 个结果, 耗时: {time2:.6f}s")
        print(f"  结果一致: {'✓' if sorted(result1) == sorted(result2) else '✗'}")
        print()

def test_custom_cases():
    """自定义测试用例"""
    print("=== 自定义测试用例 ===")
    
    while True:
        user_input = input("请输入测试字符串（输入'quit'退出）: ").strip()
        if user_input.lower() == 'quit':
            break
        
        if not user_input.isdigit():
            print("错误: 请输入只包含数字的字符串")
            continue
        
        result = solve_ip_restoration(user_input)
        print(f"输入: '{user_input}'")
        print(f"结果: {result}")
        print(f"共找到 {len(result)} 个有效IP地址")
        print()

def run_all_tests():
    """运行所有测试"""
    print("开始执行IP地址复原算法测试")
    print("=" * 50)
    
    test_basic_cases()
    test_edge_cases()
    test_performance()
    
    print("所有自动测试完成!")
    print("=" * 50)
    
    # 可选的交互式测试
    choice = input("是否进行自定义测试？(y/n): ").strip().lower()
    if choice == 'y':
        test_custom_cases()

if __name__ == "__main__":
    run_all_tests()
